package test.com.qu.myapplication.service;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/3/14.
 */

public class Api {
    private static String API_URL = "http://api.g.leonis.me/%s";
    private static AsyncHttpClient CLIENT;

    private Api() {
    }
    /**
     * 初始化网络请求
     * @param context
     */
    public static void init(Application context){
        CLIENT = null;
        AsyncHttpClient client = new AsyncHttpClient();
        Api.setHttpClient(client,context);

    }

    public static AsyncHttpClient getHttpClient() {
        return CLIENT;
    }
    public static void setHttpClient(AsyncHttpClient c,Application appliion){
        //TODO 设置头部信息
        CLIENT = c;
    }
    /**
     *  POST---请求接口
     * @param partUrl
     * @param params
     * @param handler
     */
    public static void post(String partUrl, RequestParams params,
                            AsyncHttpResponseHandler handler){
        if(CLIENT != null) {
            CLIENT.post(getAbsoluteApiUrl(partUrl), params, handler);

            Log.d("AsyncHttpClient","post:" + partUrl + "?" + params);
        }
    }
    /**
     * 消息列表
     * @param mHandler
     */
    public static void Newnotifications(AsyncHttpResponseHandler mHandler){
        Api.get("api/v1/notifications",mHandler);
    }
    public static void Login(AsyncHttpResponseHandler handler){
        String phone ="18672010446";
        String password ="123456";
        RequestParams params = new RequestParams();
        params.put("phone",phone);
        params.put("password",password);
        post("api/v1/login",params,handler);
    }
    /**
     * 拼接ApiUrl
     * @param partUrl
     * @return
     */
    public static String getAbsoluteApiUrl(String partUrl) {
        String url = partUrl;
        if (!partUrl.startsWith("http:") && !partUrl.startsWith("https:")) {
            url = String.format(API_URL, partUrl);
        }
        return url;
    }
    /**
     * GET---请求的接口
     * @param partUrl
     * @param handler
     */
    public static void get(String partUrl, AsyncHttpResponseHandler handler) {

        setTokenHeader();
        if(CLIENT != null) {
            CLIENT.get(getAbsoluteApiUrl(partUrl), handler);
        }else{
            Log.d("AsyncHttpClient","CLIENT:为空........" );
        }
        Log.d("AsyncHttpClient","post:" + partUrl);
    }
    /**
     * set Token Header
     */
    public static void setTokenHeader(){
        String Token = App.getInstance().get("Authorization");
            CLIENT.addHeader("Authorization", Token);
            Log.d("AsyncHttpClient", "Authorization:" + Token);

    }
    /**
     * 得到此时的Token------------------------------------------------------------------------------
     */
    public static void getToken(Header[] headers){
        if(headers != null)
            for(Header header : headers){
                String key = header.getName();
                String value = header.getValue();
                if(key.contains("Authorization")){
                    Log.d("Api","Authorization:"+value);
                    App.getInstance().set("Authorization",value);
                }
            }
    }

}
